package com.sanjiahantang.youjiarole.module.me.order.presenters;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.cx.pay.fenzhanglibrary.activity.BalancePayActivity;
import com.google.gson.Gson;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.PayPsdInputView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseApplication;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.module.common.models.IUserModel;
import com.sanjiahantang.youjiarole.module.common.models.impl.UserModel;
import com.sanjiahantang.youjiarole.module.common.utils.CommonModelUtils;
import com.sanjiahantang.youjiarole.module.common.views.impl.ShareSqcodeActivity;
import com.sanjiahantang.youjiarole.module.login.views.impl.LoginActivity;
import com.sanjiahantang.youjiarole.module.me.address.beans.AddressBean;
import com.sanjiahantang.youjiarole.module.me.address.beans.AddressDetailBean;
import com.sanjiahantang.youjiarole.module.me.order.adapters.OrderAdapter;
import com.sanjiahantang.youjiarole.module.me.order.adapters.OrderListAdapter;
import com.sanjiahantang.youjiarole.module.me.order.adapters.ShopListOrderAdapter;
import com.sanjiahantang.youjiarole.module.me.order.beans.CreateOrderPost;
import com.sanjiahantang.youjiarole.module.me.order.beans.GHTResultBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.LogisticsBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.OrderBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.OrderResponseBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.OrderTakeNumberResponseBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.OrderTakeResponseBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.OrderYuEPayResponseBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.Service3PartBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.ServiceListBean;
import com.sanjiahantang.youjiarole.module.me.order.beans.ShopListBean;
import com.sanjiahantang.youjiarole.module.me.order.models.IOrderModel;
import com.sanjiahantang.youjiarole.module.me.order.models.impl.OrderModel;
import com.sanjiahantang.youjiarole.module.me.order.views.OrderView;
import com.sanjiahantang.youjiarole.module.me.order.views.impl.OrderActivity;
import com.sanjiahantang.youjiarole.module.me.require.models.IRequireModel;
import com.sanjiahantang.youjiarole.module.me.require.models.impl.RequireModel;
import com.sanjiahantang.youjiarole.module.money.beans.BankCardBean;
import com.sanjiahantang.youjiarole.module.money.views.impl.MeAddBankListActivity;
import com.sanjiahantang.youjiarole.utils.KeyBoardUtils;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;



/**
 * Created by sks on 2015/9/29.
 */
public class OrderPresenter extends BaseMvpPresenter<OrderView> {

    private IOrderModel orderModel;
    private IUserModel userModel;
    private IRequireModel requireModel;
    private OrderListAdapter listAdapter;
    private OrderAdapter orderAdapter;
    private OrderView.OnLoadListener orderDetailOnLoadListener;
    private BaseView.OnLoadListener orderListOnLoadListener;
    private Dialog payDialog,payPasswordDialog;

    public OrderPresenter() {
        orderModel = new OrderModel();
        userModel = new UserModel();
        requireModel = new RequireModel();

    }


    public void loadOrderList(final Context context, int page, int tag) {
        Map<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        params.put("phoneTypeId", 1);
        params.put("typeId", tag);
        params.put(KEY_PAGE,0);
        params.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        params.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
        mView.showProgress();
        orderModel.loadOrderList(context, params, new OrderListCallBack());

    }

    public void initRecyclerView(Context context, RecyclerView recyclerView, int type) {


        if (type == OrderView.ACTIVITY_ORDER_CREATE) {
            if (orderAdapter == null) {
                orderAdapter = new OrderAdapter(context,new ArrayList<Integer>());
            }
            recyclerView.setAdapter(orderAdapter);
        }


    }

    public void loadOrder(Context context,String uaId) {
        if (uaId == null){
            Map<String,String> params = new HashMap<>();
            params.put(KEY_TOKEN,ConfigValue.DATA_KEY);
            params.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
            mView.showProgress();
            orderModel.searchUserAddressOrder(context,params, new CallBack<AddressBean>() {
                @Override
                public void onSuccess(AddressBean response, Context context, Object tag) {
                    hideProgress();
                    if (response.getCode() == RESPONSE_CODE_SUC && response.getData().size() > 0){
                        setAddress(response.getData().get(0));
                    }

                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        }else {
            Map<String,String> params = new HashMap<>();
            params.put(KEY_TOKEN,ConfigValue.DATA_KEY);
            params.put("ucaId",uaId);
            mView.showProgress();
            userModel.loadAddressDetail(context, params, new CallBack<AddressDetailBean>() {
                @Override
                public void onSuccess(AddressDetailBean response, Context context, Object tag) {
                    hideProgress();
                    if (response.getCode() == RESPONSE_CODE_SUC){
                        setAddress(response.getData());
                    }
                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        }

    }
    public void doSubmitOrder(final Context context, RecyclerView recyclerView) {
        final CreateOrderPost post = orderAdapter.getCreateOrderPost();
        if (post == null){
            return;
        }

        if (post.getUserAddressId() == 0){
            Utils.showToast(context,"请填写收货地址");
            return;
        }

        mView.showProgress();
        orderModel.createOrderForMallCommon(context,post, new CallBack<OrderResponseBean>() {
            @Override
            public void onSuccess(final OrderResponseBean response, final Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    //跳支付->回调


                    if (response.getData() != null
                            &&response.getData().getOrderInfoGoodsRaDtoList() != null
                            &&response.getData().getOrderInfoGoodsRaDtoList().size() > 0){
                        Intent intent = new Intent(context, OrderActivity.class);
                        intent.putExtra(KEY_TYPE,ORDER_TYPE_PAY);
                        Bundle b = new Bundle();
                        ShopListBean.Data data = new ShopListBean.Data();
                        data.setOrderCode(response.getData().getOrderInfoGoodsRaDtoList().get(0).getOrderCode());
                        b.putSerializable("goods",data);
                        intent.putExtra(KEY_PARAM,b);
                        context.startActivity(intent);
                        ((Activity)context).finish();
                    }

                }else{
                    if (response != null){
                        Utils.showToast(context,response.getMessage());
                    }else{
                        Utils.showToast(context,"网络错误");
                    }
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                hideProgress();
                Utils.showToast(context,"网络错误");
            }
        });
    }
    public void setInvoice(String title, String taxNo) {
        orderAdapter.setInvoice(title, taxNo);
    }

    public void setAddress(AddressBean.Data address) {
        orderAdapter.setAddress(address);
    }

    public void setOnLoadListener(OrderView.OnLoadListener onLoadListener) {
        this.orderDetailOnLoadListener = onLoadListener;
    }

    /**
     * /project/phone/plan/searchPriceDesignPhoneForRole
     *
     * @param context
     * @param id
     */
    public void getRequireOrderPrice(Context context, int id, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("requireId", String.valueOf(id));
        orderModel.searchPriceDesignPhoneForRole(context, param, new CallBack<RequireOrderBean>() {
            @Override
            public void onSuccess(RequireOrderBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getData());
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null) {
            mView.hideProgress();
        }
    }

    public void setOnLoadListener(BaseView.OnLoadListener listener) {
        this.orderListOnLoadListener = listener;
    }

    public void loadOrderTakeList(Context context, boolean flag, final BaseView.OnLoadListener onLoadListener) {
        if (TextUtils.isEmpty(ConfigValue.DATA_KEY)){
            LoginActivity.logout(context);
            return;
        }
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        Map<String,Object> map = new HashMap<>();
        map.put("currentPersonId",SPUtils.get(context,SPConfig.PHONE,"").toString());
        map.put("address",ConfigValue.city == null ? "" : ConfigValue.city);
        String role = SPUtils.get(context,SPConfig.ROLE_ID,"").toString();
        if (!TextUtils.isEmpty(role)){
            map.put("currentTypeId",role);
//            if (role.contains("0")){
//                map.put("currentTypeId",0);
//            }else if (role.contains("1")){
//                map.put("currentTypeId",1);
//            }else if (role.contains("2")){
//                map.put("currentTypeId",2);
//            }
        }else {
            Utils.showToast(context,"用户角色信息获取失败");
            return;
        }


        map.put("totalCount",100);
        param.put(KEY_PARAM,map);
//        mView.showProgress();
        if (flag){
            map.put("stateId",1);
            orderModel.searchRequireOverList(context,param, new CallBack<OrderTakeResponseBean>() {
                @Override
                public void onSuccess(OrderTakeResponseBean response, Context context, Object tag) {
                    hideProgress();
                    if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                        onLoadListener.onLoad(response.getData().getList());
                    }

                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        } else{
            map.put("stateId",0);
            orderModel.loadOrderTakeList(context,param, new CallBack<OrderTakeResponseBean>() {
                @Override
                public void onSuccess(OrderTakeResponseBean response, Context context, Object tag) {
                    hideProgress();
                    if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                        onLoadListener.onLoad(response.getData().getList());
                    }

                }

                @Override
                public void onFailure(String message) {
                    hideProgress();
                }
            });
        }

    }

    public void initSearchRequire(final Context context, final BaseView.OnLoadListener onLoadListener) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        Map<String,Object> map = new HashMap<>();
        map.put("currentPersonId",SPUtils.get(context,SPConfig.PHONE,"").toString());
        map.put("address",ConfigValue.city == null ? "" : ConfigValue.city);
        String role = SPUtils.get(context,SPConfig.ROLE_ID,"").toString();
        if (!TextUtils.isEmpty(role)){
            map.put("currentTypeId",role);
//            if (role.contains("0")){
//                map.put("currentTypeId",0);
//            }else if (role.contains("1")){
//                map.put("currentTypeId",1);
//            }else if (role.contains("2")){
//                map.put("currentTypeId",2);
//            }
        }else {
//            Utils.showToast(context,"用户角色信息获取失败");
            return;
        }

        param.put(KEY_PARAM,map);
        orderModel.initSearchRequire(context,param, new CallBack<OrderTakeNumberResponseBean>() {
            @Override
            public void onSuccess(OrderTakeNumberResponseBean response, Context context, Object tag) {
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    onLoadListener.onLoad(response.getData());
                }

            }

            @Override
            public void onFailure(String message) {
                Utils.showToast(context,"获取接单数据失败");
            }
        });
    }

    public void findBidRequireInfoForRole(final Context context, int id, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("ucriId",String.valueOf(id));
        if (mView == null) return;
        mView.showProgress();
        requireModel.findBidRequireInfoForRole(context,param, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    if (response.getData() != null && response.getData().contains("true")){
                        listener.onLoad(null);
                    }else if (response.getData() != null && response.getData().contains("false")){
                        Utils.showToast(context,"温馨提示，投标人数已满");
                    }else {
                        Utils.showToast(context,"服务器开小差了");
                    }
                }else if (response != null){
                    Utils.showToast(context,response.getMessage());
                }else{
                    Utils.showToast(context,"服务器开小差了");
                }
            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                Utils.showToast(context,"服务器开小差了");
            }
        });
    }

    public void findOrderExpressInfo(Context context, String expressCode, String expressNumber, final BaseView.OnLoadListener listener) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("phoneTypeId",1);
        param.put("expressCode",expressCode);
        param.put("expressNumber",expressNumber);
        orderModel.findOrderExpressInfo(context,param, new CallBack<StringBean>() {
            @Override
            public void onSuccess(StringBean response, Context context, Object tag) {
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    LogisticsBean bean = new Gson().fromJson("{\"data\":" + response.getData() + "}",LogisticsBean.class);
                    if (bean != null){
                        LogisticsBean.Data data = bean.getData();
                        listener.onLoad(data);
                    }
                }


            }

            @Override
            public void onFailure(String message) {

            }
        });
    }

    public void searchServiceOrder(Context context, String orderCode, final BaseView.OnLoadListener listener) {
        Map<String,String> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("phoneTypeId","1");
        param.put("orderCode",orderCode);
        mView.showProgress();
        orderModel.searchServiceOrder(context,param, new CallBack<ServiceListBean>() {
            @Override
            public void onSuccess(ServiceListBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    listener.onLoad(response.getItem());
                }

            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
            }
        });
    }

    public void createOrderForDesign(Context context, String orderCode, final BaseView.OnLoadListener listener) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("phoneTypeId",1);
        param.put("orderCode",orderCode);

        mView.showProgress();
        orderModel.createOrderForDesign(context,param, new CallBack<Service3PartBean>() {
            @Override
            public void onSuccess(Service3PartBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    if (response.getData() != null
                            && response.getData().getOrderServiceRaDtoList() != null
                            && response.getData().getOrderServiceRaDtoList().size() > 0){
                        listener.onLoad(response.getData().getOrderServiceRaDtoList().get(0));
                    }

                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    public void goodsBalancePay(final Context context, Object pay_data, final BaseView.OnLoadListener onLoadListener) {
        if (pay_data != null){
            orderModel.goodsBalancePay(context,pay_data,new CallBack<BaseBean>(){

                @Override
                public void onSuccess(BaseBean response, Context context, Object tag) {
                    Log.e("------",new Gson().toJson(response));
                    if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                        onLoadListener.onLoad(null);
                    }else if (response != null){
                        Utils.showToast(context,response.getMessage());
                    }else{
                        Utils.showToast(context,"支付失败");
                    }
                }

                @Override
                public void onFailure(String message) {
                    Utils.showToast(context,message);
                }
            });
        }
    }


    //商品订单
    private class OrderListCallBack implements CallBack<ShopListBean> {

        @Override
        public void onSuccess(ShopListBean response, Context context, Object tag) {
            hideProgress();

            ShopListOrderAdapter shopListOrderAdapter = new ShopListOrderAdapter(context);
            shopListOrderAdapter.setData(response.getData());
            Object[] param = new Object[2];
            param[0] = CALLBACK_TYPE_RECYCLER_ADAPTER;
            param[1] = shopListOrderAdapter;
            orderListOnLoadListener.onLoad(param);
        }

        @Override
        public void onFailure(String message) {
            hideProgress();
        }
    }
    public void setData(OrderBean.Data data,ArrayList<Integer> typeList,ShopListBean.Data orderInfo){
        orderAdapter.setData(data,typeList,orderInfo,this);
        orderAdapter.notifyDataSetChanged();
    }

    public void operatorState(final Context context, int stateId, String orderRecordCode, final String orderCode) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("typeId",0);
        param.put("stateId",stateId);
        param.put("phoneTypeId",1);
        param.put("orderCode",orderRecordCode);
        mView.showProgress();
        orderModel.operatorState(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC){
                    searchGoodsRaForCode(context,(String) tag,orderListOnLoadListener);
                }else{
                    Utils.showToast(context,"修改状态失败了");
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                searchGoodsRaForCode(context,orderCode,orderListOnLoadListener);
            }
        },orderCode);
    }

    public void searchGoodsRaForCode(Context context, String orderCode, BaseView.OnLoadListener onLoadListener){
        this.orderListOnLoadListener = onLoadListener;
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put("phoneTypeId",1);
        param.put("orderCode",orderCode);
        mView.showProgress();
        orderModel.searchGoodsRaForCode(context, param, new CallBack<ShopListBean>() {
            @Override
            public void onSuccess(ShopListBean response, Context context, Object tag) {
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                    Object[] para = new Object[2];
                    para[0] = CALLBACK_TYPE_ORDER_DATA;
                    para[1] = response.getItem();
                    orderListOnLoadListener.onLoad(para);
                }else{
                    Object[] para = new Object[2];
                    para[0] = CALLBACK_TYPE_FAILURE;
                    para[1] = "失败";
                    orderListOnLoadListener.onLoad(para);
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Object[] para = new Object[2];
                para[0] = CALLBACK_TYPE_FAILURE;
                para[1] = message;
                orderListOnLoadListener.onLoad(para);
            }
        });
    }

    public void doPay(final Context context, final ShopListBean.Data bean){
        if (payDialog == null){
            payDialog = new Dialog(context, R.style.Dialog_Fullscreen);
            payDialog.setContentView(R.layout.dialog_select_pay_way);
            TextView txtNeedPrice = (TextView) payDialog.findViewById(R.id.txtNeedPrice);
            txtNeedPrice.setText(String.format("%.2f",bean.getPayResult()));
            final RadioButton rbWX = (RadioButton) payDialog.findViewById(R.id.rbWX);
            final RadioButton rbZFB = (RadioButton) payDialog.findViewById(R.id.rbZFB);
            final RadioButton rbBank = (RadioButton) payDialog.findViewById(R.id.rbBank);
            final RadioButton rbBankWeb = (RadioButton) payDialog.findViewById(R.id.rbBankWeb);
            final RadioButton rbPos = (RadioButton) payDialog.findViewById(R.id.rbPos);
            final RadioButton rbBalance = (RadioButton) payDialog.findViewById(R.id.rbBalance);
            payDialog.findViewById(R.id.btnOK).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    long nowTime = System.currentTimeMillis();
                    if (Math.abs(ConfigValue.nowTime - nowTime) < DOUBLE_CLICK_TIME_SPACE){
                        return;
                    }
                    ConfigValue.nowTime = nowTime;
                    if(rbWX.isChecked()){
                        Utils.showToast(context,"很遗憾，微信暂未接入");
                    }else if(rbZFB.isChecked()){
                        Utils.showToast(context,"很遗憾，支付宝暂未接入");
                    }else if(rbBalance.isChecked()){
                        payDialog.dismiss();
                        bean.setToken(ConfigValue.DATA_KEY);
                        bean.setPhoneTypeId(1);
                        orderModel.createGoodsPayNumber(context,bean, new CallBack<OrderYuEPayResponseBean>() {

                            @Override
                            public void onSuccess(OrderYuEPayResponseBean response, Context context, Object tag) {
                                Intent intent = new Intent();
                                intent.setClass(context,BalancePayActivity.class);
                                intent.putExtra("userId",response.getData().getUserId());
                                intent.putExtra("privateKey",response.getData().getPrivateKey());
                                intent.putExtra("publicKey",response.getData().getPublicKey());
                                intent.putExtra("rechargeAmt",response.getData().getRechargeAmt());
                                intent.putExtra("mOrderNo",response.getData().getMOrderNo());
                                intent.putExtra("orgId",response.getData().getOrgId());
                                intent.putExtra("stageOrderId",response.getData().getStageOrderId());
                                ((Activity)context).startActivityForResult(intent,1000);
                            }

                            @Override
                            public void onFailure(String message) {
                                Utils.showToast(context,message);
                            }
                        });
                    }else if(rbBank.isChecked()){
                        payDialog.dismiss();
                        bean.setToken(ConfigValue.DATA_KEY);
                        bean.setPhoneTypeId(1);
                        if (mView == null) return;
                        mView.showProgress();
                        orderModel.getCheckCodeGood(context,bean, new CallBack<ShopListBean>() {
                            @Override
                            public void onSuccess(ShopListBean response, Context context, Object tag) {
                                if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                                    if (mView == null) return;
                                    mView.hideProgress();
                                    if (response.getPayVoBean() != null){
                                        bean.setPayVo(response.getPayVoBean());
                                        Utils.showToast(context,"验证码已发送");
                                        payPasswordDialog.show();
                                    }else{
                                        Utils.showToast(context,"异常了");
                                    }
                                }else if (response != null){
                                    Utils.showToast(context,response.getMessage());
                                }else{
                                    Utils.showToast(context,"异常了");
                                }
                            }

                            @Override
                            public void onFailure(String message) {
                                if (mView == null) return;
                                mView.hideProgress();
                                Utils.showToast(context,"异常了");
                            }
                        });
                    } else if(rbBankWeb.isChecked()){
                        payDialog.dismiss();
                        bean.setToken(ConfigValue.DATA_KEY);
                        bean.setPhoneTypeId(1);
                        if (mView == null) return;
                        mView.showProgress();
                        orderModel.saveBankPayGoods(context,bean, new CallBack<ShopListBean>() {
                            @Override
                            public void onSuccess(ShopListBean response, Context context, Object tag) {
                                if (mView == null) return;
                                mView.hideProgress();
                                if (response != null){
                                    Utils.showToast(context,response.getMessage());
                                }else{
                                    Utils.showToast(context,"异常了");
                                }
                            }

                            @Override
                            public void onFailure(String message) {
                                if (mView == null) return;
                                mView.hideProgress();
                                Utils.showToast(context,"异常了");
                            }
                        });
                    } else if(rbPos.isChecked()){
                        payDialog.dismiss();
                        bean.setToken(ConfigValue.DATA_KEY);
                        bean.setPhoneTypeId(1);
                        if (mView == null) return;
                        mView.showProgress();
                        orderModel.saveBankPayGoods(context,bean, new CallBack<ShopListBean>() {
                            @Override
                            public void onSuccess(ShopListBean response, Context context, Object tag) {
                                if (mView == null) return;
                                mView.hideProgress();
                                if (response != null){
                                    String content = response.getString();
                                    if (!TextUtils.isEmpty(content)){
                                        content = content.replace("\"","");
                                        ShareSqcodeActivity.openQRPage(context,content,"POS机付款");
                                    }else{
                                        Utils.showToast(context,"生成POS二维码失败");
                                    }
                                }else{
                                    Utils.showToast(context,"异常了");
                                }
                            }

                            @Override
                            public void onFailure(String message) {
                                if (mView == null) return;
                                mView.hideProgress();
                                Utils.showToast(context,"异常了");
                            }
                        });
                    } else {
                        Utils.showToast(context,"请选择支付方式");
                    }
                }
            });

            payDialog.findViewById(R.id.vWX).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vZFB).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBank).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(true);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBankWeb).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(true);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vPos).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(true);
                    rbBalance.setChecked(false);
                }
            });

            payDialog.findViewById(R.id.vBalance).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(true);
                }
            });

            rbWX.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(true);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(false);
                }
            });

            rbZFB.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(true);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(false);
                }
            });

            rbBank.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(true);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(false);
                }
            });

            rbBankWeb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(true);
                    rbPos.setChecked(true);
                    rbBalance.setChecked(false);
                }
            });

            rbPos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(true);
                    rbBalance.setChecked(false);
                }
            });

            rbBalance.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rbWX.setChecked(false);
                    rbZFB.setChecked(false);
                    rbBank.setChecked(false);
                    rbBankWeb.setChecked(false);
                    rbPos.setChecked(false);
                    rbBalance.setChecked(true);
                }
            });
        }

        if (payPasswordDialog == null){
            payPasswordDialog = new Dialog(context,R.style.dialog_common);
            View payPasswordView = LayoutInflater.from(context).inflate(R.layout.dialog_pay_password,null);
            final PayPsdInputView passwordInputView = (PayPsdInputView) payPasswordView.findViewById(R.id.passwordView);
            payPasswordDialog.setContentView(payPasswordView);
            passwordInputView.setComparePassword( new PayPsdInputView.onPasswordListener() {

                @Override
                public void onDifference(String oldPsd, String newPsd) {

                }

                @Override
                public void onEqual(String psd) {
                }

                @Override
                public void inputFinished(final String inputPsd) {
                    bean.setToken(ConfigValue.DATA_KEY);
                    bean.setPhoneTypeId(1);
                    bean.setPayPwd(inputPsd);
                    ShopListBean.PayVo payVoBean = bean.getPayVo();
                    if (payVoBean != null){
                        payVoBean.setCheckCode(inputPsd);
                        bean.setPayVo(payVoBean);
                    }
                    passwordInputView.cleanPsd();
                    KeyBoardUtils.closeKeybordOnly((Activity) context);
                    if (mView == null) return;
                    mView.showProgress();
                    orderModel.payGoodsOrderPhone(context,bean, new CallBack<BaseBean>() {
                        @Override
                        public void onSuccess(BaseBean response, Context context, Object tag) {
                            payPasswordDialog.dismiss();
                            hideProgress();
                            passwordInputView.cleanPsd();
                            if (response != null && response.getCode() == RESPONSE_CODE_SUC){
                                Utils.showToast(context,"支付成功");
                                BaseApplication.clearActivity();
                                ((Activity)context).finish();
                            }else if(response != null){
                                Utils.showToast(context,response.getMessage());
                            }else{
                                Utils.showToast(context,"很遗憾，支付失败了");
                            }
                        }

                        @Override
                        public void onFailure(String message) {
                            hideProgress();
                            payPasswordDialog.dismiss();
                            passwordInputView.cleanPsd();
                        }
                    });
                }
            });
        }

        CommonModelUtils.searchBankCardDefault(context, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                if (content != null && content instanceof BankCardBean.Item){
                    BankCardBean.Item item = (BankCardBean.Item) content;
                    final TextView tvBank = (TextView) payDialog.findViewById(R.id.tvBank);
                    tvBank.setText(String.format("%s(尾号%s)",item.getBankGenneralName(),Utils.getLast4(item.getBankCardNo())));
                    payDialog.show();
                }else{
                    MeAddBankListActivity.startActivity(context);
                    payDialog.dismiss();
                }
            }
        });
    }


    public void doCancel(final Context context, final ShopListBean.Data data) {
        Utils.showDialog((Activity) context, "提示", "是否取消订单?", "", "", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()){
                    case R.id.txtDialogSure:
                        Map<String,Object> param = new HashMap<>();
                        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
                        param.put("phoneTypeId",1);
                        param.put("secondOrderCode",data.getOrderCode());
                        param.put("typeId","0");
                        orderModel.cancelOrderPhone(context,param, new CallBack<BaseBean>() {
                            @Override
                            public void onSuccess(BaseBean response, Context context, Object tag) {
                                hideProgress();
                                if (response.getCode() == RESPONSE_CODE_SUC){
                                    ((Activity)context).finish();
                                }
                                Utils.showToast(context,response.getMessage());
                            }

                            @Override
                            public void onFailure(String message) {
                                hideProgress();
                            }
                        });
                        break;
                    default:
                        Utils.dismissDialog();
                        break;
                }

            }
        });
    }
}
